package com.fingard.dsp.bank.directbank.fjnx12;

import com.fingard.constant.Format;
import com.fingard.dsp.bank.directbank.DirectBase;
import com.fingard.dsp.bank.directbank.FailCode;
import com.fingard.net.ITcpClient;
import com.fingard.net.TcpPortClient;
import com.fingard.net.TcpReceiver;
import com.fingard.text.StringHelper;
import com.fingard.util.TransIDHelper;

import java.util.Date;


public class FJNX12Base extends DirectBase implements ITcpClient {
    private static TransIDHelper idHelper = new TransIDHelper(1, 999);

    protected String getQryTransID() {
        return Format.DateTime14Format.format(new Date()) + idHelper.getNextIDStr();
    }

    protected String getCharset() {
        return getBankConfig().getCharset("GBK");
    }

    protected String[] sendToBank(String tmpReqStr, String transCode) throws Exception {
        String[] retStr = new String[]{"", ""};
        String charSet = getCharset();
        int readTimeout = getReadTimeout();
        WriteBankLogLn("readTimeout:" + readTimeout / 1000 + "秒");
        WriteBankLogLn2("提交银行的地址：" + bankFront.serverIP + ":" + bankFront.serverPort);
        if (StringHelper.isNullOrEmpty(bankFront.serverIP) || StringHelper.isNullOrEmpty(bankFront.serverPort)) {
            retStr[0] = FailCode.E9981;
            retStr[1] = "未设置前置机的serverIP或serverPort";
        } else {
            TcpPortClient tmpClient = new TcpPortClient(bankFront.serverIP, Integer.parseInt(bankFront.serverPort), this);
            tmpClient.timeoutMil = readTimeout;
            byte[] tmpBytesBody = tmpReqStr.getBytes(charSet);

            String reqStrLength = String.format("%-6s", tmpBytesBody.length);
            String trdCode = String.format("%-4s",transCode);
            String keyVersion = getBankConfig().getValueAsString("keyVersion");
            String tmpHead = reqStrLength + trdCode + keyVersion;
            byte[] tmpBytesHead = tmpHead.getBytes(charSet);

            byte[] tmpBytesSend = new byte[tmpBytesHead.length + tmpBytesBody.length];
            System.arraycopy(tmpBytesHead, 0, tmpBytesSend, 0, tmpBytesHead.length);
            System.arraycopy(tmpBytesBody, 0, tmpBytesSend, tmpBytesHead.length, tmpBytesBody.length);

            WriteBankLogStep2(tmpHead + tmpReqStr);

            TcpReceiver tmpReceiver = tmpClient.sendMessage(tmpBytesSend);
            if (StringHelper.isNullOrEmpty(tmpReceiver.connState)) {
                String tmpBkStr = new String(tmpReceiver.recBytes, 0, tmpReceiver.readedLength, charSet);
                WriteBankLogStep3(tmpBkStr);
            }
            retStr[0] = tmpReceiver.connState;
            if (tmpReceiver.isCompleted) {
                retStr[1] = new String(tmpReceiver.recBytes, tmpReceiver.contentStartIndex, tmpReceiver.contentLength, charSet);
            } else {
                retStr[1] = tmpReceiver.getConnMsg();
                WriteBankLogLn(retStr[1]);
            }
        }
        return retStr;
    }


    @Override
    public void getContentLength(TcpReceiver p_receiver) throws Exception {
        if (p_receiver.contentLength == -1 && p_receiver.readedLength >= 6) {
            String tmpHead = new String(p_receiver.recBytes, 0, 6, getCharset());
            p_receiver.contentLength = Integer.parseInt(tmpHead.trim());
            p_receiver.contentStartIndex = 26;
        }
    }
}
